package com.xhs.main.controller.rsb;

import com.xhs.main.entity.rsb.*;
import com.xhs.main.service.rsb.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author author
 * @create 2020-10-24-14:10
 */
@Controller
public class RsbApplyController {
    @Autowired
    RsbApplyServiceImpl rsbApplyService;
    @Autowired
    RsbAttendanceServiceImpl rsbAttendanceService;
    @Autowired
    RsbMoneyServiceImpl rsbMoneyService;
    @Autowired
    RsbUserServiceImpl rsbUserService;
    @Autowired
    RsbContractServiceImpl rsbContractService;
    @Autowired
    RsbShowMoneyServiceImpl rsbShowMoneyService;

    @ResponseBody
    @RequestMapping("/insertApply")
    public void insertApply(RsbApply rsbApply) {
        rsbApply.setRsbApStatus("未审批");
        rsbApplyService.insertApply(rsbApply);
    }

    @ResponseBody
    @RequestMapping("/deleteApply")
    public void deleteApply(String rsbApNum){
        rsbApplyService.deleteApply(rsbApNum);
    }

    //已通过
    @ResponseBody
    @RequestMapping("/updateApply")
    public void updateApply(String rsbApNum) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = format.format(date);
        rsbApplyService.updateApply(rsbApNum,format1);

        //考勤
        RsbApply rsbApply = rsbApplyService.selectByRsbApNum(rsbApNum);
        String userName = rsbApply.getUserName();
        List<RsbApply> rsbApplies = rsbApplyService.selectByUserName(userName);
        //加班时长
        int overTime=0;
        //缺勤时间
        int longs=0;
        for(int i=0;i<rsbApplies.size();i++){
            if(!rsbApplies.get(i).getRsbApOvertime().equals("")&&rsbApplies.get(i).getRsbApOvertime()!=null){
                int overTime2 = Integer.parseInt(rsbApplies.get(i).getRsbApOvertime());
                overTime=overTime+overTime2;
            }
            if(!rsbApplies.get(i).getRsbApLong().equals("")&&rsbApplies.get(i).getRsbApLong()!=null){
                int longs2 = Integer.parseInt(rsbApplies.get(i).getRsbApLong());
                longs=longs+longs2;
            }
        }
        //加班时间总和
        rsbAttendanceService.updateAttendance(userName+"kq",overTime);
        //缺勤时间总和
        rsbAttendanceService.updateAttendance2(userName+"kq",22-longs,longs);

        int longs3=0;
        //更改工资表
        List<RsbApply> rsbApplies1 = rsbApplyService.selectNotIn(userName);
        rsbApplies1.forEach(e->{
            System.out.println(e);
        });
        if(rsbApplies1!=null){
            for(int i=0;i<rsbApplies1.size();i++){
                if(!rsbApplies1.get(i).getRsbApLong().equals("")&&rsbApplies1.get(i).getRsbApLong()!=null){
                    int longs2 = Integer.parseInt(rsbApplies1.get(i).getRsbApLong());
                    longs3=longs3+longs2;
                }
            }
        }
        //得到加班费、缺勤扣款
        RsbUser rsbUser = rsbUserService.selectRsbUserByName(userName);
        RsbMoney rm = rsbMoneyService.selectChoose(rsbUser.getUserDep(), rsbUser.getUserPost()).get(0);

        //把要改的值放进对象中
        RsbShowMoney rsm = new RsbShowMoney();
        rsm.setRsbShAdd(overTime*rm.getRsbMonAdd());
        rsm.setRsbShNocome(longs3*rm.getRsbMonNocome());

        RsbContract rc = rsbContractService.selectByUserName(userName);
        rsm.setRsbShBeforetaxes(Double.parseDouble(rc.getRsbConBasepay())+rsm.getRsbShAdd()-rsm.getRsbShNocome()+rm.getRsbMonAdd()+rm.getRsbMonEat()+rm.getRsbMonTraffic()+rm.getRsbMonPhone()-rm.getRsbMonFive());
        rsm.setRsbShAftertaxes(rsm.getRsbShBeforetaxes()*0.9);
        rsm.setRsbShGetmoney(rsm.getRsbShAftertaxes());
        rsbShowMoneyService.updateShowMoney(rsm,rsbApply.getUserNum());
    }
    //未通过
    @ResponseBody
    @RequestMapping("/updateApply2")
    public void updateApply2(String rsbApNum) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = format.format(date);
        rsbApplyService.updateApply2(rsbApNum,format1);
    }

    @ResponseBody
    @RequestMapping("/showAllApply")
    public RsbLayui showAllApply() {
        List<RsbApply> rsbApplies = rsbApplyService.selectAllApply();
        /*for(RsbApply r:rsbApplies){
            System.out.println(r);
        }*/
        return new RsbLayui(0,"",1000,rsbApplies);
    }

/*========================================考勤表===============================================*/
    //删除考勤表
    @ResponseBody
    @RequestMapping("/deleteRsbAttendance")
    public void deleteRsbAttendance(String rsbAtNum){
        rsbAttendanceService.deleteAttendance(rsbAtNum);
    }
    //缺勤表
    @ResponseBody
    @RequestMapping("/selectByUserName")
    public RsbLayui selectByUserName(String userName) {
        List<RsbApply> rsbApplies = rsbApplyService.selectByUserName(userName);
        return new RsbLayui(0,"",1000,rsbApplies);
    }
    //考勤表
    @ResponseBody
    @RequestMapping("/selectAllAttendance")
    public RsbLayui selectAllAttendance() {
        List<RsbAttendance> rsbAttendances = rsbAttendanceService.selectAll();
        return new RsbLayui(0,"",1000,rsbAttendances);
    }
    //展示员工工资
    @ResponseBody
    @RequestMapping("/selectAllShowMoney")
    public RsbLayui selectAllShowMoney() {
        List<RsbShowMoney> rsbShowMonies = rsbShowMoneyService.selectAllShowMoney();
        return new RsbLayui(0,"",1000,rsbShowMonies);
    }






}
